﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CayleyTree
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                th1 = (int)this.numericUpDown6.Value * Math.PI / 180;
                th2 = (int)this.numericUpDown5.Value * Math.PI / 180;
                per1 = double.Parse(this.domainUpDown3.Text);
                per2 = double.Parse(this.domainUpDown2.Text);
                
                if (graphics == null) graphics = this.panel2.CreateGraphics();
                graphics.Clear(Color.White);
                drawCayleyTree((int)this.numericUpDown1.Value, 200, 310, (int)this.numericUpDown2.Value, -Math.PI / 2);
                this.label8.Text = "成功！";
            }
            catch (Exception)
            {
                this.label8.Text = "出错！";
            }
        }

        private Graphics graphics;
        double th1 = 30 * Math.PI / 180;
        double th2 = 20 * Math.PI / 180;
        double per1 = 0.6;
        double per2 = 0.7;

        void drawCayleyTree(int n,
                double x0, double y0, double leng, double th)
        {
            if (n == 0) return;
            
            double x1 = x0 + leng * Math.Cos(th);
            double y1 = y0 + leng * Math.Sin(th);

            drawLine(x0, y0, x1, y1);

            drawCayleyTree(n - 1, x1, y1, per1 * leng, th + th1);
            drawCayleyTree(n - 1, x1, y1, per2 * leng, th - th2);
        }
        void drawLine(double x0, double y0, double x1, double y1)
        {
            Pen color;
            if (this.domainUpDown1.Text == "绿色") color = Pens.Green;
            else if (this.domainUpDown1.Text == "黑色") color = Pens.Black;
            else if (this.domainUpDown1.Text == "红色") color = Pens.Red;
            else color = Pens.Blue;

            graphics.DrawLine(
                color,
                (int)x0, (int)y0, (int)x1, (int)y1);
        }

 

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void button3_Click(object sender, EventArgs e)
        {

        }

        private void label7_Click(object sender, EventArgs e)
        {

        }

        private void label6_Click(object sender, EventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void label4_Click(object sender, EventArgs e)
        {

        }

        private void numericUpDown4_ValueChanged(object sender, EventArgs e)
        {

        }

        private void numericUpDown1_ValueChanged(object sender, EventArgs e)
        {

        }

        private void label5_Click(object sender, EventArgs e)
        {

        }

        private void domainUpDown1_SelectedItemChanged(object sender, EventArgs e)
        {

        }

        private void domainUpDown3_SelectedItemChanged(object sender, EventArgs e)
        {

        }

        private void label8_Click(object sender, EventArgs e)
        {

        }

        private void panel2_Paint(object sender, PaintEventArgs e)
        {

        }
    }
}
